package com.baitiaojun.video.api.impl;

import com.alibaba.fastjson.JSONObject;
import com.baitiaojun.api.annotation.UserAuthoritiesAccess;
import com.baitiaojun.api.domain.user.User;
import com.baitiaojun.api.domain.user.UserInfo;
import com.baitiaojun.api.domain.video.CollectionGroup;
import com.baitiaojun.api.exception.ConditionException;
import com.baitiaojun.api.util.ConstantUtil;
import com.baitiaojun.api.vo.CollectionGroupWrapper;
import com.baitiaojun.api.vo.JsonRes;
import com.baitiaojun.video.api.CollectionGroupApi;
import com.baitiaojun.video.feign.UserFeign;
import com.baitiaojun.video.service.CollectionGroupService;
import com.baitiaojun.video.support.UserSupport;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @类名 CollectionGroupApiImpl
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2022/12/24 18:30
 * @版本 1.0
 */
@RestController
public class CollectionGroupApiImpl implements CollectionGroupApi {

    @Resource
    private UserSupport userSupport;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private CollectionGroupService collectionGroupService;

    @Autowired
    private UserFeign userFeign;

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes addCollectionGroup(CollectionGroup collectionGroup) throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        User user = JSONObject.parseObject(JSONObject.toJSONString(userSupport.getCurrentUser(request)), User.class);
        String nick = user.getUserInfo().getNick();
        collectionGroupService.addCollectionGroup(collectionGroup, userId, nick);
        return JsonRes.builder().success();
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes updateCollectionGroup(CollectionGroup collectionGroup) throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        collectionGroupService.updateCollectionGroup(collectionGroup, userId);
        return JsonRes.builder().success();
    }

    @UserAuthoritiesAccess(
            accessRole = {ConstantUtil.ROLE_LV0
                    ,ConstantUtil.ROLE_LV1
                    ,ConstantUtil.ROLE_LV2
                    ,ConstantUtil.ROLE_LV3
                    ,ConstantUtil.ROLE_LV4
                    ,ConstantUtil.ROLE_LV5
                    ,ConstantUtil.ROLE_LV6
            })
    @Override
    public JsonRes cancelCollectionGroup(Long groupId) throws Exception {
        Long userId = userSupport.getCurrentUserId(request);
        collectionGroupService.cancelCollectionGroup(groupId, userId);
        return JsonRes.builder().success();
    }

    @Override
    public JsonRes getCollectionGroups(Long userInfoId) throws Exception {
        Long userId;
        String nick;
        if (ObjectUtils.isEmpty(userInfoId)) {
            userId = userSupport.getCurrentUserId(request);
            User user = JSONObject.parseObject(JSONObject.toJSONString(userSupport.getCurrentUser(request)), User.class);
            nick = user.getUserInfo().getNick();
        } else {
            JsonRes jsonRes = userFeign.getOtherUserInfo(userInfoId);
            if(jsonRes.get("status").equals(500)) {
                throw new ConditionException("无法获取用户信息");
            }
            User user = JSONObject.parseObject(JSONObject.toJSONString(jsonRes.get("data")), User.class);
            UserInfo userInfo = user.getUserInfo();
            userId = userInfo.getUserid();
            nick = userInfo.getNick();
        }
        List<CollectionGroupWrapper> collectionGroupWrappers
                = collectionGroupService.getCollectionGroupContents(userId, nick);
        return JsonRes.builder().success().data(collectionGroupWrappers);
    }
}